package hivMultiCompartment;

import hivMultiCompartment.TCell.AgentState;



public class CD4TCell extends TCell implements SusceptibleCell {
	public boolean infected;
	public Virus bearingVirus;
	public double reactivationRate;
	
	public CD4TCell() {
		super ();
		avgLife = GlobalConstants.avgLifeCD4TNaive;
		life = GlobalConstants.sampleNormal(avgLife);
		moveInterval = GlobalConstants.moveIntervalCD4T;
		
		probEnterLymph = GlobalConstants.probEnterLymphCD4Naive;
		probEnterThymus = probEnterLymph + GlobalConstants.probEnterThymusCD4Naive;
		probExit = GlobalConstants.probExitCD4TNaive;

		limitedPorliferation = false;
		proliferateCount = GlobalConstants.dividingCountCD4TNaive;
		proliferateInterval = GlobalConstants.doublingTimeCD4TNaive;
		proliferateCD = proliferateInterval;
		
		setColor(GlobalConstants.colorCD4TNaive);
		
		infected = false;
		bearingVirus = null;
		reactivationRate = GlobalConstants.reactivationRateCD4TResting;
		
		recognizeStrain = Model.getUniformIntFromTo(1, GlobalConstants.maxIdiotypeCD4T);
	}
	
	public boolean isSusceptible() {
		boolean susceptible = false;
		if (!infected) {
			susceptible = true;
		}
		return susceptible;
	}
	public void getInfected(Virus v) {
		infected = true;
		bearingVirus = v;
		presentStrain = v.myStrain;
		v.infectHostCell(); 
	}
	
	public void statesAndInteractions(){
		if(!alive) return;
		if(infected) {
			//reactivate
			double r = Model.getUniformDoubleFromTo(0, 1);
			if(r < reactivationRate){
				bearingVirus.reactivate(getMyOrgan(), x, y);
				agentDie();
			}
		}
		if(Organ2D != null) {
			if(infected) { 
				//activate CD8 memory
				activateMatchingCD8Memory();
				//search for CD8 effector and get killed
				double r2 = Model.getUniformDoubleFromTo(0, 1);
				if(r2 < probKilledByMatchingCD8TEffector()) {
					agentDie();
				}
			}else {
				Virus v = getALocalVirus();
				if (v != null) {
					getInfected(v);
				}
			}
		}
		switch (state){
			case ACTIVE:
				if(proliferateCount == 0){
					double r = Model.getUniformDoubleFromTo(0, 1);
					if(r < GlobalConstants.memoryFrqCD4T){
						changeState(AgentState.MEMORY);
					}else {
						changeState(AgentState.EFFECTOR);
					}
				}
				break;
//			case EFFECTOR:
//				break;
//			case MEMORY:
//				break;
			default:
				break;
		}
	}
	public void activate(){
		changeState(AgentState.ACTIVE);
	}
	
	public void changeState(AgentState s) {
		state = s;
		switch(s){
			case ACTIVE:
				avgLife = GlobalConstants.avgLifeCD4TActive;
				life = GlobalConstants.sampleNormal(avgLife);
				probExit = GlobalConstants.probExitCD4TActive;
				probEnterLymph = GlobalConstants.probEnterLymphCD4Active;
				probEnterThymus = GlobalConstants.probEnterThymusCD4Active;
				proliferateCount = GlobalConstants.dividingCountCD4TActive;
				proliferateInterval = GlobalConstants.doublingTimeCD4TActive;
				proliferateCD = proliferateInterval;
				reactivationRate = GlobalConstants.reactivationRateCD4TActive;
				limitedPorliferation = true;
				break;
			case EFFECTOR:
				avgLife = GlobalConstants.avgLifeCD4TEffector;
				life = GlobalConstants.sampleNormal(avgLife);
				probExit = GlobalConstants.probExitCD4TEffector;
				probEnterLymph = GlobalConstants.probEnterLymphCD4Effector;
				probEnterThymus = GlobalConstants.probEnterThymusCD4Effector;
				limitedPorliferation = true;
				proliferateCount = 0;
				break;
			case MEMORY:
				avgLife = GlobalConstants.avgLifeCD4TMemory;
				life = GlobalConstants.sampleNormal(avgLife);
				probExit = GlobalConstants.probExitCD4TMemory;
				probEnterLymph = GlobalConstants.probEnterLymphCD4Memory;
				probEnterThymus = GlobalConstants.probEnterThymusCD4Memory;
				proliferateCount = 0;
				reactivationRate = GlobalConstants.reactivationRateCD4TResting;
				limitedPorliferation = true;
				break;
			default:
				//naive
				break;
		}
	}
}
